package ui.list 
{
	import com.GCInventory;
	import com.GCItem;
	import data.GCItemData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	import ui.button.GCButton;
	import com.greensock.TweenLite;
	import com.greensock.easing.Quad;
	
	/**
	 * ...
	 * @author Jason Won
	 */
	public class GCSalesScrollList extends ScrollList
	{	
		private var _inventory:GCInventory;					// Inventory currently connected to this list
		private var _filterItemType:uint;					// A filter for showing only item of this type
		private var _categoryType:uint;						// Category of the item
		private var _displayVector:Vector.<GCItem>;			// Vector that holds the items on display
		private var _filterButtons:Sprite;					// A sprite with buttons to switch the filter
		private var _filterButtonMask:Shape;				// Mask for the filter buttons
		private var _filterResourceType:uint;				// Sets the filter type for resources
		
		// Filter buttons
		private var _allButton:GCButton;
		private var _foodButton:GCButton;
		private var _lumberButton:GCButton;
		private var _ironButton:GCButton;
		private var _silverButton:GCButton;
		private var _copperButton:GCButton;
		private var _woolButton:GCButton;
		private var _cottonButton:GCButton;
		private var _silkButton:GCButton;
		private var _saltButton:GCButton;
		private var _spicesButton:GCButton;
		private var _oilButton:GCButton;
		private var _coalButton:GCButton;
		private var _recipesButton:GCButton;
		private var _upgradeButton:GCButton;
		private var _arrowKnob:GCButton;
		private var _buttonDetailArea:Sprite;
		private var _buttonDetail:TextField;
		
		/**
		 * Constructor
		 * @param	width			Width of the scroll list
		 * @param	height			Height of the scroll list
		 */
		public function GCSalesScrollList(width:int, height:int, inventory:GCInventory = null) 
		{
			super(width, height);
			_inventory = inventory;
			loadInventory(inventory);
			_displayVector = new Vector.<GCItem>();
			_filterButtons = new Sprite();
			
			// Create the buttons
			_allButton = new GCButton(onClickButtonFilter);
			_foodButton = new GCButton(onClickButtonFilter);
			_lumberButton = new GCButton(onClickButtonFilter);
			_ironButton = new GCButton(onClickButtonFilter);
			_silverButton = new GCButton(onClickButtonFilter);
			_copperButton = new GCButton(onClickButtonFilter);
			_woolButton = new GCButton(onClickButtonFilter);
			_cottonButton = new GCButton(onClickButtonFilter);
			_silkButton = new GCButton(onClickButtonFilter);
			_saltButton = new GCButton(onClickButtonFilter);
			_spicesButton = new GCButton(onClickButtonFilter);
			_oilButton = new GCButton(onClickButtonFilter);
			_coalButton = new GCButton(onClickButtonFilter);
			_recipesButton = new GCButton(onClickButtonFilter);
			_upgradeButton = new GCButton(onClickButtonFilter);
			_arrowKnob = new GCButton(null);
			_buttonDetailArea = new Sprite();
			_buttonDetail = new TextField();
			
			initializeSalesScrollList();
		}
		
		private function initializeSalesScrollList():void
		{
			// Draw the button filter sprite
			_filterButtons.graphics.beginFill(GCConstants.COLOR_BLACK, 0.5);
			_filterButtons.graphics.drawRect(0, 0, 40, componentHeight);
			_filterButtons.graphics.endFill();			
			_filterButtons.addEventListener(MouseEvent.ROLL_OVER, onRollOverButtonFilter);
			_filterButtons.addEventListener(MouseEvent.ROLL_OUT, onRollOutButtonFilter);
			_filterButtons.x = componentWidth - 10;
			_filterButtons.y = 0;
			
			// Mask the button filter
			_filterButtonMask = new Shape();
			_filterButtonMask.graphics.beginFill(0xffffff, 1);
			_filterButtonMask.graphics.drawRoundRect(0, 0, componentWidth, componentHeight, 20);
			_filterButtonMask.x = 0;
			_filterButtonMask.y = 0;
			_filterButtons.mask = _filterButtonMask;
			addChild(_filterButtonMask);
			
			// Load Graphics and position the buttons
			_allButton.loadGraphic(GCResources.goal, 20, 20, false);
			_allButton.addEventListener(MouseEvent.ROLL_OVER, onRollOverFilterButton);
			_allButton.addEventListener(MouseEvent.ROLL_OUT, onRollOutFilterButton);
			_allButton.x = 10;
			_allButton.y = 5;
			_foodButton.loadGraphic(GCResources.food, 20, 20, false);
			_foodButton..addEventListener(MouseEvent.ROLL_OVER, onRollOverFilterButton);
			_foodButton.x = _allButton.x;
			_foodButton.y = _allButton.y + _allButton.height + 5;
			_lumberButton.loadGraphic(GCResources.lumber, 20, 20, false);
			_lumberButton.addEventListener(MouseEvent.ROLL_OVER, onRollOverFilterButton);
			_lumberButton.x = _foodButton.x;
			_lumberButton.y = _foodButton.y + _foodButton.height + 5;
			_ironButton.loadGraphic(GCResources.iron, 20, 20, false);
			_ironButton.addEventListener(MouseEvent.ROLL_OVER, onRollOverFilterButton);
			_ironButton.x = _lumberButton.x;
			_ironButton.y = _lumberButton.y + _lumberButton.height + 5;
			_silverButton.loadGraphic(GCResources.silver, 20, 20, false);
			_silverButton.addEventListener(MouseEvent.ROLL_OVER, onRollOverFilterButton);
			_silverButton.x = _ironButton.x;
			_silverButton.y = _ironButton.y + _ironButton.height + 5;
			_copperButton.loadGraphic(GCResources.copper, 20, 20, false);
			_copperButton.addEventListener(MouseEvent.ROLL_OVER, onRollOverFilterButton);
			_copperButton.x = _silverButton.x;
			_copperButton.y = _silverButton.y + _silverButton.height + 5;			
			_woolButton.loadGraphic(GCResources.wool, 20, 20, false);
			_woolButton.addEventListener(MouseEvent.ROLL_OVER, onRollOverFilterButton);
			_woolButton.x = _copperButton.x;
			_woolButton.y = _copperButton.y + _copperButton.height + 5;
			_cottonButton.loadGraphic(GCResources.cotton, 20, 20, false);
			_cottonButton.addEventListener(MouseEvent.ROLL_OVER, onRollOverFilterButton);
			_cottonButton.x = _woolButton.x;
			_cottonButton.y = _woolButton.y + _woolButton.height + 5;
			_silkButton.loadGraphic(GCResources.silk, 20, 20, false);
			_silkButton.addEventListener(MouseEvent.ROLL_OVER, onRollOverFilterButton);
			_silkButton.x = _cottonButton.x;
			_silkButton.y = _cottonButton.y + _cottonButton.height + 5;
			_saltButton.loadGraphic(GCResources.salt, 20, 20, false);
			_saltButton.addEventListener(MouseEvent.ROLL_OVER, onRollOverFilterButton);
			_saltButton.x = _silkButton.x;
			_saltButton.y = _silkButton.y + _silkButton.height + 5;
			_spicesButton.loadGraphic(GCResources.spices, 20, 20, false);
			_spicesButton.addEventListener(MouseEvent.ROLL_OVER, onRollOverFilterButton);
			_spicesButton.x = _saltButton.x;
			_spicesButton.y = _saltButton.y + _saltButton.height + 5;
			_oilButton.loadGraphic(GCResources.oil, 20, 20, false);
			_oilButton.addEventListener(MouseEvent.ROLL_OVER, onRollOverFilterButton);
			_oilButton.x = _spicesButton.x;
			_oilButton.y = _spicesButton.y + _spicesButton.height + 5			
			_coalButton.loadGraphic(GCResources.coal, 20, 20, false);
			_coalButton.addEventListener(MouseEvent.ROLL_OVER, onRollOverFilterButton);
			_coalButton.x = _oilButton.x;
			_coalButton.y = _oilButton.y + _oilButton.height + 5;
			_recipesButton.loadGraphic(GCResources.recipe, 20, 20, false);
			_recipesButton.addEventListener(MouseEvent.ROLL_OVER, onRollOverFilterButton);
			_recipesButton.x = _coalButton.x;
			_recipesButton.y = _coalButton.y + _coalButton.height + 5;
			_upgradeButton.loadGraphic(GCResources.plus, 20, 20, false);
			_upgradeButton.addEventListener(MouseEvent.ROLL_OVER, onRollOverFilterButton);
			_upgradeButton.x = _recipesButton.x;
			_upgradeButton.y = _recipesButton.y + _recipesButton.height + 5;
			
			// Arrow knob to show that the filter buttons exists
			_arrowKnob.loadGraphic(GCResources.left_arrow, 20, 20);
			_arrowKnob.x = -10;
			_arrowKnob.y = (componentHeight - _arrowKnob.height)/ 2;
			
			// Create the button hover detail
			_buttonDetail.text = "";
			_buttonDetail.textColor = GCConstants.COLOR_WHITE;
			_buttonDetail.selectable = false;
			_buttonDetailArea.addChild(_buttonDetail);
			_buttonDetailArea.visible = false;
			
			// Add all the buttons to the button filter sprite
			_filterButtons.addChild(_allButton);
			_filterButtons.addChild(_foodButton);
			_filterButtons.addChild(_lumberButton);
			_filterButtons.addChild(_ironButton);
			_filterButtons.addChild(_silverButton);
			_filterButtons.addChild(_copperButton);
			_filterButtons.addChild(_woolButton); 
			_filterButtons.addChild(_cottonButton); 
			_filterButtons.addChild(_silkButton);
			_filterButtons.addChild(_saltButton); 
			_filterButtons.addChild(_spicesButton);
			_filterButtons.addChild(_oilButton);
			_filterButtons.addChild(_coalButton);
			_filterButtons.addChild(_recipesButton);
			_filterButtons.addChild(_upgradeButton);
			_filterButtons.addChild(_arrowKnob);
			this.addChild(_filterButtons);
			this.addChild(_buttonDetailArea);
		}
		
		
		//----------------------------------------------------------------------
		//	PUBLIC FUNCTIONS
		//----------------------------------------------------------------------		
		
		/**
		 * Loads the sales in the given inventory into this list
		 * @param	inventory
		 */
		public function loadInventory(inventory:GCInventory):void
		{
			// Remove all previous items
			removeAll();
			_inventory = inventory;
			_displayVector = new Vector.<GCItem>();
			
			var item:GCItem;
			var itemUI:GCItemListUI;
			
			// Fill the display list with all the items in the inventory
			if (_inventory != null && _inventory.inventory != null)
			{
				for each (item in _inventory.inventory)
				{
					if (filterItem(GCItemData.getTypeFromId(item.itemId), item.itemId))
					{
						_displayVector.push(item);
					}
				}
				
				// Sort the items in the display list
				//sortItems();
				
				// Add a visual UI element for each item in the display list
				for each (item in _displayVector)
				{
					itemUI = new GCItemListUI(item);
					addDisplay(itemUI);				
				}
			}
		}
		
		/**
		 * Adds the given sales to the list
		 * @param	newSale
		 */
		public function addSale(newSale:GCItem):void
		{			
			// Check for the item in your inventory
			if (_inventory != null)			
			{
				var sales:Dictionary = _inventory.inventory;
				var sale:GCItem = sales[newSale.invItemId]
				
				if (sale != null)
				{
				// It exists
					// Update the exisiting quantity
					sale.quantity += newSale.quantity;
				}
				else
				{
					// Item doesn't exist; Create the new item
					sales[newSale.itemId] = newSale;
					_displayVector.push(newSale);
					var newSaleUI:GCItemListUI = new GCItemListUI(newSale);
					addDisplay(newSaleUI);	
				}
				
				// Redraw the list
				redraw();
			}
			else
			{
				// We don't have an inventory
				return; // Or should we add to visual anyway?
			}
		}
		
		/**
		 * Pushes the new item into the inventory and updates visuals if not filtered out
		 * @param	newSale
		 */
		public function pushNewSale(newSale:GCItem):void
		{
			if (_inventory != null && _inventory.inventory != null)
			{
				_inventory.inventory[newSale.invItemId] = newSale;
				if (filterItem(GCItemData.getTypeFromId(newSale.itemId), newSale.itemId))
				{
					_displayVector.push(newSale);
					var newSaleUI:GCItemListUI = new GCItemListUI(newSale);
					addDisplay(newSaleUI);
				}
			}
		}
		
		
		/**
		 * Removes the given sales from the list
		 * @param	sale
		 */
		public function removeSale(sale:GCItem):void
		{
			if (sale == null)
				return;
				
			if (_inventory != null)
			{
				var inventory:Dictionary = _inventory.inventory;
				
				// Check that the item is in the inventory
				if (inventory[sale.invItemId] == null)	
				{
					// Item doesn't exist; exit
					return;
				} 
				else 
				{
					//Item does exist; Remove item from inventory
					delete inventory[sale.invItemId];
					_inventory.currentCapcity -= sale.quantity;
				}
				
				var index:int = _displayVector.indexOf(sale);				
				// Check for the item in the display
				if (index == -1)
				{
					// Display doesn't exist; exit
					return;
				}
				else
				{
					// Display exists; Remove the display
					_displayVector.splice(index, 1);
					
					redraw();					
				}				
			}
		}		
		
		/**
		 * Redraws the list
		 */
		public function redraw():void
		{
			// Remove all previous items
			super.removeAll();
			
			// Refill the display based on the vector
			var item:GCItem;
			var itemUI:GCItemListUI;
			for each(item in _displayVector)
			{
				itemUI = new GCItemListUI(item);
				addDisplay(itemUI);
			}
		}		
		
		/**
		 * Clears out all sales from the list
		 */
		override public function removeAll():void
		{
			super.removeAll();
			_inventory = null;
			_displayList = null;
			_selected = null;
		}
		
		//----------------------------------------------------------------------
		//	PRIVATE FUNCTIONS
		//----------------------------------------------------------------------
		
		private function sortItems():void
		{
			_displayVector.sort(			
				function compare(item1:GCItem, item2:GCItem):Number
				{
					if (item1.itemId < item2.itemId)
						return -1;
					else if (item1.itemId > item2.itemId)
						return 1;
					else 
					{
						if (item1.price < item2.price)
							return -1
						else if (item1.price > item2.price)
							return 1;
					}
					return 0;		
				});
		}
		
		private function sortPrices():void
		{
			_displayVector.sort(
				function compare(item1:GCItem, item2:GCItem):Number
				{
					if (item1.price < item2.price)
						return -1;
					else if (item1.price > item2.price)
						return 1;
					else 
						return 0;
				});			
		}
		
		/**
		 * Checks if the item is being filtered
		 * @return
		 */
		private function filterItem(itemType:uint, itemId:uint):Boolean
		{
			// Filter set to show everything
			if (_filterItemType == GCItemData.ITEM_TYPE_GENERAL)
				return true;
			// Filter set to only show resources
			else if (_filterItemType == GCItemData.ITEM_TYPE_RESOURCE)
			{
				// Check that the item is of type resource
				if (itemType == GCItemData.ITEM_TYPE_RESOURCE)
				{
					// Check that the item is the correct resource type
					if (_filterResourceType == GCItemData.ITEM_NULL)
						return true;
					else if (itemId == _filterResourceType)
						return true;
				}
				else
					return false
			}
			// Filter set to only show recipes
			else if (_filterItemType == GCItemData.ITEM_TYPE_RECIPE || _filterItemType == GCItemData.ITEM_TYPE_SHIP_RECIPE)
			{
				if (itemType == GCItemData.ITEM_TYPE_RECIPE || itemType == GCItemData.ITEM_TYPE_SHIP_RECIPE)
					return true;
				else 
					return false;
			}
			// Filter set to show only upgrades
			else if (_filterItemType == GCItemData.ITEM_TYPE_UPGRADE)
			{
				if (itemType == GCItemData.ITEM_TYPE_UPGRADE)
					return true;
				else
					return false;
			}
			// Else, type doesn't match anything
			return false;
		}
		
		//----------------------------------------------------------------------
		//	EVENT HANDLERS
		//----------------------------------------------------------------------

		private function onClickButtonFilter(event:MouseEvent):void
		{
			switch(event.target)
			{
				case _allButton:
					_filterItemType = GCItemData.ITEM_TYPE_GENERAL;
					_filterResourceType = GCItemData.ITEM_NULL;					
					break;
				case _foodButton:
					_filterItemType = GCItemData.ITEM_TYPE_RESOURCE;
					_filterResourceType = GCItemData.ITEM_FOOD;
					break;
				case _lumberButton:
					_filterItemType = GCItemData.ITEM_TYPE_RESOURCE;
					_filterResourceType = GCItemData.ITEM_LUMBER;
					break;
				case _ironButton:
					_filterItemType = GCItemData.ITEM_TYPE_RESOURCE;
					_filterResourceType = GCItemData.ITEM_IRON;
					break;
				case _silverButton:
					_filterItemType = GCItemData.ITEM_TYPE_RESOURCE;
					_filterResourceType = GCItemData.ITEM_SILVER;
					break;
				case _copperButton:
					_filterItemType = GCItemData.ITEM_TYPE_RESOURCE;
					_filterResourceType = GCItemData.ITEM_COPPER;
					break;
				case _woolButton:
					_filterItemType = GCItemData.ITEM_TYPE_RESOURCE;
					_filterResourceType = GCItemData.ITEM_WOOL;
					break;
				case _cottonButton:
					_filterItemType = GCItemData.ITEM_TYPE_RESOURCE;
					_filterResourceType = GCItemData.ITEM_COTTON;
					break;
				case _silkButton:
					_filterItemType = GCItemData.ITEM_TYPE_RESOURCE;
					_filterResourceType = GCItemData.ITEM_SILK;
					break;
				case _saltButton:
					_filterItemType = GCItemData.ITEM_TYPE_RESOURCE;
					_filterResourceType = GCItemData.ITEM_SALT;
					break;
				case _spicesButton:
					_filterItemType = GCItemData.ITEM_TYPE_RESOURCE;
					_filterResourceType = GCItemData.ITEM_SPICES;
					break;
				case _oilButton:
					_filterItemType = GCItemData.ITEM_TYPE_RESOURCE;
					_filterResourceType = GCItemData.ITEM_OIL;
					break;
				case _coalButton:
					_filterItemType = GCItemData.ITEM_TYPE_RESOURCE;
					_filterResourceType = GCItemData.ITEM_COAL;
					break;
				case _recipesButton:
					_filterItemType = GCItemData.ITEM_TYPE_RECIPE;
					_filterResourceType = GCItemData.ITEM_NULL;
					break;
				case _upgradeButton:
					_filterItemType = GCItemData.ITEM_TYPE_UPGRADE;
					_filterResourceType = GCItemData.ITEM_NULL;
					break;
				default:
					_filterItemType = GCItemData.ITEM_TYPE_GENERAL;
					_filterResourceType = GCItemData.ITEM_NULL;
					break;
			}
			
			super.removeAll();
			_displayVector = new Vector.<GCItem>();			
			var item:GCItem;
			var itemUI:GCItemListUI;
			
			if (_inventory != null && _inventory.inventory != null)
			{
				for each (item in _inventory.inventory)
				{
					if (filterItem(GCItemData.getTypeFromId(item.itemId), item.itemId))
					{
						_displayVector.push(item);
					}
				}
			}
			
			// Sort all the sales by price
			sortPrices();
			
			// Add a visual UI element for each item in the display list
			for each (item in _displayVector)
			{
				itemUI = new GCItemListUI(item);
				addDisplay(itemUI);				
			}
		}
		
		private function onRollOverFilterButton(event:MouseEvent):void
		{
			var button:GCButton = event.target as GCButton;
			switch(event.target)
			{
				case _allButton:
					_buttonDetail.text = "All Items";
					break;
				case _foodButton:
					_buttonDetail.text = "Food";
					break;
				case _lumberButton:
					_buttonDetail.text = "Lumber";
					break;
				case _ironButton:
					_buttonDetail.text = "Iron";
					break;
				case _silverButton:
					_buttonDetail.text = "Silver";
					break;
				case _copperButton:
					_buttonDetail.text = "Copper";
					break;
				case _woolButton:
					_buttonDetail.text = "Wool";
					break;
				case _cottonButton:
					_buttonDetail.text = "Cotton";
					break;
				case _silkButton:
					_buttonDetail.text = "Silk";
					break;
				case _saltButton:
					_buttonDetail.text = "Salt";
					break;
				case _spicesButton:
					_buttonDetail.text = "Spice";
					break;
				case _oilButton:
					_buttonDetail.text = "Oil";
					break;
				case _coalButton:
					_buttonDetail.text = "Coal";
					break;
				case _recipesButton:
					_buttonDetail.text = "Recipes";
					break;
				case _upgradeButton:
					_buttonDetail.text = "Upgrades";
					break;
				default:
					return;
			}
			_buttonDetailArea.x = componentWidth - button.x;
			_buttonDetailArea.y = button.y - button.height / 2;
			_buttonDetailArea.visible = true;
			
		}
		
		private function onRollOutFilterButton(event:MouseEvent):void
		{
			_buttonDetailArea.visible = false;
		}
		
		private function onRollOverButtonFilter(event:MouseEvent):void
		{
			TweenLite.to(_filterButtons, 0.1, { x:(componentWidth - _filterButtons.width + 10), ease:Quad.easeOut } );
			_arrowKnob.visible = false;
		}
		
		private function onRollOutButtonFilter(event:MouseEvent):void
		{
			TweenLite.to(_filterButtons, 0.1, { x:(componentWidth - 10), ease:Quad.easeOut } );
			_arrowKnob.visible = true;
			_buttonDetailArea.visible = false;
		}
		
	}
}

